package Classes.GameViews.ViewPanels
{
	
	import Classes.GameDatas.GD_Player;
	import Classes.GameDatas.GD_Shop;
	import Classes.GameViews.ViewScenes.ViewScene;
	import Classes.Managers.M_ConfigData;
	import Classes.staticinfo.StaticInfo;
	
	import UIComponents.FlatInfo;
	
	import fl.events.ComponentEvent;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.system.ApplicationDomain;
	
	public class VP_StorePanel extends ViewPanel
	{
		private static var _instance:VP_StorePanel;
		private var _interfaceUI:Sprite;
		public function VP_StorePanel()
		{
			this.init();
		}
		
		private function init():void
		{
			this._interfaceUI=new (ApplicationDomain.currentDomain.getDefinition("UIStore_v") as Class);
			this.addChild(this.interfaceUI);
			this.addListener();
			this.addObserveFuncs();
		}
		
		/**
		 * 注册事件 
		 * 
		 */		
		public function addListener():void
		{
			this.addEventListener("close",closeHandler);
			this.addEventListener("showExChangePanel",showExChangeHandler);
		}
		
		private function closeHandler(e:Event):void
		{
			this.close();
		}
		
		private function showExChangeHandler(e:Event):void
		{
			VP_exChangePanel.instance.openPanel();
		}
		
		override public function openAt(vs:ViewScene,bg:Boolean=false,sd:Boolean=true):void
		{
			if(FlatInfo.newGuiderShop)
			{
				(this.interfaceUI["_jbSelectBtn"] as DisplayObject).dispatchEvent(new ComponentEvent(ComponentEvent.BUTTON_DOWN));
				(this.interfaceUI["_button_weapon"] as DisplayObject).dispatchEvent(new ComponentEvent(ComponentEvent.BUTTON_DOWN));
			}
			super.openAt(vs,true,sd);
			setStoreData();
			this.toCenter();
		}
		
		/**
		 * 注册监听 
		 * 
		 */		
		public function addObserveFuncs():void
		{
			this.addObserveFunc(GD_Player.instance,"gold",showMyOB);
			this.addObserveFunc(GD_Player.instance,"money",showMyJB);
			this.addObserveFunc(GD_Shop.instance,StaticInfo.DELETE_SHOP_ITEM,setStoreData,false);
			this.addObserveFunc(GD_Player.instance,"QQVip",controlQQVip);
		}
		
		public function controlQQVip():void
		{
			this._interfaceUI["controlMoenyIcon"](M_ConfigData.instance.isOpenQQVip);
		}
		
		public function setStoreData():void
		{
		     this.interfaceUI["getAllStoreData"](GD_Shop.instance.shopItems);
		}
		
		private function showMyJB():void
		{
			this.interfaceUI["showMyJB"](GD_Player.instance.money);
		}
		
		private function showMyOB():void
		{
			this.interfaceUI["showMyOB"](GD_Player.instance.gold);
		}
		
		
		/*getter & setter*/
		public static function get instance():VP_StorePanel
		{
			if(!_instance)_instance=new VP_StorePanel;
			return _instance;
		}
		
		public function get interfaceUI():Sprite
		{
			return _interfaceUI;
		}
	}
}